import * as dayjs from 'dayjs'
import { normError } from '@/api/api.js'
export default {
  data() {
    return {
      limit: 15,
      scrollabled: true,
      windowScrollTop: 0,
      isCurrentView: false
    }
  },
  computed: {
    curuser() {
      return this.$store.state.CURRENTUSER || {}
    },
    isIosWechat() {
      const ua = navigator.userAgent
      const wechatUA = ua.toLowerCase().match(/MicroMessenger/i)
      const isIos = ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
      const isWechat = wechatUA !== null && wechatUA.toString() === 'micromessenger'
      return isIos && isWechat && this.$route.query.jxytime === undefined
    }
  },
  watch: {
    scrollabled(val) {
      const el = document.scrollingElement
      if (val) {
        document.body.style.top = ''
        document.body.classList.remove('m-pop-open')
        el && (el.scrollTop = this.windowScrollTop)
      } else {
        document.body.classList.add('m-pop-open')
        this.windowScrollTop = el ? el.scrollTop : 0
        document.body.style.top = -this.windowScrollTop + 'px'
      }
    }
  },
  methods: {
    goBack() {
      const fallIndex = this.isIosWechat ? 2 : 1
      window.history.length <= fallIndex ? this.$router.replace('/') : this.$router.back()
    },
    /**
     * 定位到锚点
     * @param {string} selector
     */
    goAnchor(selector) {
      const anchor = this.$el.querySelector(selector)
      try {
        const rect = anchor.getBoundingClientRect()
        const scrollTop = document.documentElement.scrollTop
        document.scrollingElement.scrollTop = rect.top + scrollTop
      } catch (error) {
        // eslint-disable-next-line no-console
        console.warn('锚点定位失败: ', { selector, anchor, error })
      }
    },
    showApiError(error, defaultMessage = null) {
      error = normError(error)
      let { message } = error
      const { canceled } = error

      if (!message && defaultMessage) {
        message = defaultMessage
      }

      if (!canceled && message) {
        this.showError(message)
      }
    },
    goTo(params) {
      this.$router.push(params)
    },
    serverMessage(message, defaultMessage) {
      if (typeof message === 'string') {
        return message
      }
      for (let key in message) {
        return this.messageBundle(message[key] || defaultMessage)
      }
      return defaultMessage
    },
    messageBundle(message) {
      if (Array.isArray(message)) {
        const [firstMessage] = message

        return firstMessage
      }
      return message
    }
  },
  filters: {
    formtTime(t, fmt = 'YYYY/MM/DD HH:mm') {
      if (typeof t === 'number') {
        t = t * 1000
      }
      if (!t || t === undefined) return t
      return dayjs(t).format(fmt)
    },
    localTime(value) {
      if (value) {
        value = new Date(
          value
            .replace(/-/g, '/')
            .replace('T', ' ')
            .replace('Z', '')
        )
        value = value.getTime() - value.getTimezoneOffset() * 60 * 1000
      } else {
        return '-'
      }
      return new Date(value).toLocaleString(navigator.language, { hour12: false })
    }
  }
}
